{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "google",
   "metadata": {},
   "source": [
    "##### Copyright 2025 Google LLC."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "apache",
   "metadata": {},
   "source": [
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "    http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "basename",
   "metadata": {},
   "source": [
    "# linear_assignment_api"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "link",
   "metadata": {},
   "source": [
    "<table align=\"left\">\n",
    "<td>\n",
    "<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/examples/linear_assignment_api.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
    "</td>\n",
    "<td>\n",
    "<a href=\"https://github.com/google/or-tools/blob/main/examples/python/linear_assignment_api.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
    "</td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "doc",
   "metadata": {},
   "source": [
    "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "install",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install ortools"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "description",
   "metadata": {},
   "source": [
    "\n",
    "Test linear sum assignment on a 4x4 matrix.\n",
    "\n",
    "   Example taken from:\n",
    "   http://www.ee.oulu.fi/~mpa/matreng/eem1_2-1.htm with kCost[0][1]\n",
    "   modified so the optimum solution is unique.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "code",
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import Sequence\n",
    "from ortools.graph.python import linear_sum_assignment\n",
    "\n",
    "\n",
    "def run_assignment_on_4x4_matrix():\n",
    "    \"\"\"Test linear sum assignment on a 4x4 matrix.\"\"\"\n",
    "    num_sources = 4\n",
    "    num_targets = 4\n",
    "    cost = [[90, 76, 75, 80], [35, 85, 55, 65], [125, 95, 90, 105], [45, 110, 95, 115]]\n",
    "    expected_cost = cost[0][3] + cost[1][2] + cost[2][1] + cost[3][0]\n",
    "\n",
    "    assignment = linear_sum_assignment.SimpleLinearSumAssignment()\n",
    "    for source in range(0, num_sources):\n",
    "        for target in range(0, num_targets):\n",
    "            assignment.add_arc_with_cost(source, target, cost[source][target])\n",
    "\n",
    "    solve_status = assignment.solve()\n",
    "    if solve_status == assignment.OPTIMAL:\n",
    "        print(\"Successful solve.\")\n",
    "        print(\"Total cost\", assignment.optimal_cost(), \"/\", expected_cost)\n",
    "        for i in range(0, assignment.num_nodes()):\n",
    "            print(\n",
    "                \"Left node %d assigned to right node %d with cost %d.\"\n",
    "                % (i, assignment.right_mate(i), assignment.assignment_cost(i))\n",
    "            )\n",
    "    elif solve_status == assignment.INFEASIBLE:\n",
    "        print(\"No perfect matching exists.\")\n",
    "    elif solve_status == assignment.POSSIBLE_OVERFLOW:\n",
    "        print(\"Some input costs are too large and may cause an integer overflow.\")\n",
    "\n",
    "\n",
    "def main(argv: Sequence[str]) -> None:\n",
    "    if len(argv) > 1:\n",
    "        raise app.UsageError(\"Too many command-line arguments.\")\n",
    "    run_assignment_on_4x4_matrix()\n",
    "\n",
    "\n",
    "main()\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
